<template>
  <div class="mod-poap__poapsourcecode">
    <el-dialog v-model="state.drawerCode" title="源代码" fullscreen :before-close="handleClose">
      <!--    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">-->
      <!--&lt;!&ndash;      <el-form-item>&ndash;&gt;-->
      <!--&lt;!&ndash;        <el-input v-model="state.dataForm.name" placeholder="代码名称" clearable></el-input>&ndash;&gt;-->
      <!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
      <!--&lt;!&ndash;      <el-form-item>&ndash;&gt;-->
      <!--&lt;!&ndash;        <el-select v-model="state.dataForm.licenseId" placeholder="开源许可证id">&ndash;&gt;-->
      <!--&lt;!&ndash;          <el-option label="选择" value="0"></el-option>&ndash;&gt;-->
      <!--&lt;!&ndash;        </el-select>&ndash;&gt;-->
      <!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
      <!--&lt;!&ndash;      <el-form-item>&ndash;&gt;-->
      <!--&lt;!&ndash;        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>&ndash;&gt;-->
      <!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
      <!--      <el-form-item>-->
      <!--        <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item>-->
      <!--        <el-button v-if="state.hasPermission('poap:poapsourcecode:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item>-->
      <!--        <el-button v-if="state.hasPermission('poap:poapsourcecode:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>-->
      <!--      </el-form-item>-->
      <!--    </el-form>-->
      <el-table v-loading="state.dataListLoading" :data="state.dataList" border
        @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
        <!--      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
        <el-table-column prop="code.name" label="代码名称" header-align="center" align="center"></el-table-column>
        <el-table-column prop="code.url" label="代码文件地址" header-align="center" align="center"></el-table-column>
        <el-table-column prop="code.description" label="算法描述" header-align="center" align="center"></el-table-column>
        <el-table-column prop="code.language" label="编程语言" header-align="center" align="center"></el-table-column>
        <el-table-column prop="code.licenseDTO.name" label="开源许可证" header-align="center" align="center"></el-table-column>
        <!--      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">-->
        <!--        <template v-slot="scope">-->
        <!--          <el-button type="primary" link @click="addCode(scope.row.id)">添加至项目</el-button>-->
        <!--          <el-button v-if="state.hasPermission('poap:poapsourcecode:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>-->
        <!--          <el-button v-if="state.hasPermission('poap:poapsourcecode:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
      </el-table>
      <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
        :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
        @current-change="state.pageCurrentChangeHandle"> </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update :key="addKey" :projectId="props.projectId" ref="addOrUpdateRef"
        @refreshDataList="state.getDataList"></add-or-update>
      <template v-slot:footer>
        <!--        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>-->
        <!--        <el-button type="primary" @click="visible = false">{{ $t("confirm") }}</el-button>-->
        <el-button type="info" color="#666dfd" @click="handleClose" size="large" class="closeBtn">点击关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./poapsourcecode-add-or-update.vue";
import { ElMessageBox } from "element-plus";
import baseService from "@/service/baseService";

const props = defineProps({
  projectId: String,
  projectStatus: Number
});

const visible = ref(false);

const view = reactive({
  getDataListURL: "/poap/poapprojectcode/page",
  getDataListIsPage: true,
  exportURL: "/poap/poapsourcecode/export",
  deleteURL: "/poap/poapsourcecode",
  deleteIsBatch: true,
  drawerCode: false,
  dataForm: {
    name: "",
    licenseId: "",
    projectId: "" as any
  },
  codeIds: [] as any[]
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};

const initCode = () => {
  state.drawerCode = true;
  state.dataForm.projectId = props.projectId;
  // if (props.projectStatus === 1 || props.projectStatus === 2) this.buttonShow = false;
  refresh();
};
const refresh = () => {
  state.getDataList();
};

const emit = defineEmits(["getCodeId"]);
const handleClose = () => {
  ElMessageBox.confirm("确定要关闭吗？", { confirmButtonText: "确认", cancelButtonText: "取消" }).then(() => {
    if (state.dataList) {
      for (let i = 0; i < state.dataList.length; i++) {
        state.codeIds[i] = state.dataList[i].id;
      }
      emit("getCodeId", state.codeIds);
    }
    state.drawerCode = false;
  });
};

const addCode = (id: string) => {
  baseService
    .get("/poap/poappage/addCode", {
      projectId: props.projectId,
      codeId: id
    })
    .then((res) => {
      console.log(res);
      if (res.code != 0) return false;
      ElMessageBox({
        type: "success",
        message: "添加成功"
      });
      state.getDataList();
    });
};

defineExpose({ initCode });
</script>
<style>
:deep(.closeBtn) {
  margin: 0 auto;
}
/* /deep/ .closeBtn {
  margin: 0 auto;
} */
</style>
